import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { useGoodStore } from '@/stores/counter'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue')
    },
    {
      path: '/insertGoods',
      name: 'insertGoods',     
      component: () => import('../views/Goods/insertGoods.vue')
    },
    {
      path: '/showGoods',
      name: 'showGoods',     
      component: () => import('../views/Goods/showGoods.vue')
    },
    {
      path: '/backGoods',
      name: 'backGoods',     
      component: () => import('../views/Goods/backGoods.vue')
    },
    {
      path: '/addUser',
      name: 'addUser',     
      component: () => import('../views/Users/addUser.vue')
    },
    {
      path: '/showUser',
      name: 'showUser',     
      component: () => import('../views/Users/showUser.vue')
    },
    {
      path: '/editUser',
      name: 'editUser',     
      component: () => import('../views/Users/editUser.vue')
    },
    {
      path: '/showRole',
      name: 'showRole',     
      component: () => import('../views/Role/showRole.vue')
    },
    {
      path: '/addPermisison',
      name: 'addPermisison',     
      component: () => import('../views/Permission/addPermission.vue')
    },
    {
      path: '/showPermission',
      name: 'showPermission',     
      component: () => import('../views/Permission/showPermission.vue')
    },
    {
      path: '/editPermission',
      name: 'editPermission',     
      component: () => import('../views/Permission/editPermission.vue')
    },
    {
      path: '/RolePermisisonUpdate',
      name: 'RolePermisisonUpdate',     
      component: () => import('../views/Role/RolePermisisonUpdate.vue')
    },
    {
      path: '/Login',
      name: 'Login',     
      component: () => import('../views/Users/Login.vue')
    },
    {
      path: '/showOrder',
      name: 'showOrder',     
      component: () => import('../views/Order/showOrder.vue')
    },
    {
      path: '/editOrder',
      name: 'editOrder',     
      component: () => import('../views/Order/editOrder.vue')
    },
    {
      path: '/paySuccess',
      name: 'paySuccess',     
      component: () => import('../views/Order/paySuccess.vue')
    }
  ]
})
//导航守卫
router.beforeEach((to:any,from:any,next)=>{
  console.log('到哪去'+to.name)
  console.log('从哪来'+from.name)

//必须在导航守卫定义仓库
const useStore=useGoodStore();

// if(to.name=='Login')
// {
//   next()
// }
// else
// {
//   if(useStore.isLogin)
//   {
//     next();
//   }
//   else
//   {
//     next({name:'Login'})
//   }
// }
console.log(useStore.isLogin);
if(to.name=='paySuccess')
{
  next();
  return
}

if(to.name!='Login'&&!useStore.isLogin)
{
  next({name:'Login'})
  return
}
else
{
  next()
}
})

export default router
